<template>
  <el-dialog
    :title="formData.id ? '编辑部门' : '新增部门'"
    :visible="visible"
    width="30%"
    @close="closeFn"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="depForm"
      label-width="120px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
        >
          <el-option
            v-for="item in employeesList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('update:visible', false)">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartment, editDepartment } from '@/api/department'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    currentNode: {
      type: Object,
      required: true,
      default: () => {}
    },
    originDepts: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data() {
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formRules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '部门名称需要为1-50位字符组成',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              let isRepeat = false
              if (this.formData.id) {
                isRepeat = this.originDepts
                  .filter((item) => {
                    return (
                      item.pid === this.formData.pid &&
                      item.id !== this.formData.id
                    )
                  })
                  .some((item) => item.name === value)
              } else {
                isRepeat = this.currentNode.children?.some(
                  (item) => item.name === value
                )
              }

              isRepeat ? callback(new Error('部门名称不能重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '部门名称需要为1-50位字符组成',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              console.log(this.originDepts)
              const isRepeat = this.originDepts.some((item) => {
                return item.code === value
              })
              isRepeat ? callback(new Error('部门编码不能重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '管理员名称不能为空',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门简介不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 300,
            message: '部门名称需要为1-300位字符组成',
            trigger: 'blur'
          }
        ]
      },
      employeesList: []
    }
  },
  methods: {
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employeesList = res
    },
    closeFn() {
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.depForm.resetFields()
      this.$emit('update:visible', false)
    },
    onSubmit() {
      this.$refs.depForm.validate(async(isOk) => {
        if (isOk) {
          if (this.formData.id) {
            await editDepartment(this.formData)
          } else {
            await addDepartment({ ...this.formData, pid: this.currentNode.id })
          }
          this.$emit('updateDeps')
          this.closeFn()
        }
      })
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  text-align: center;
  display: inline-block;
  width: 100%;
}
</style>
